package com.zhuoyian.analysis.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

/**
 * ��App��صĸ�����?
 */
/**
 * @author Administrator
 * 
 */
public class AppUtils {

	private AppUtils() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");

	}

	/**
	 */
	public static String getAppName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			int labelRes = packageInfo.applicationInfo.labelRes;
			return context.getResources().getString(labelRes);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 
	 */
	public static String getVersionName(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packageInfo.versionName;

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					context.getPackageName(), 0);
			return packageInfo.versionCode;

		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 */
	public static String getIMEI(Context context) {
		try {
			TelephonyManager telecomManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = telecomManager.getDeviceId();
			if(imei==null){
				imei ="";
			}
			return imei;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 */
	public static String getIMSI(Context context) {
		try {
			TelephonyManager telecomManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = telecomManager.getSubscriberId();
			if(imei==null){
				imei = "";
			}
			return imei;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @param context
	 * @return
	 */
	public static String getPhoneNumber(Context context) {
		try {
			TelephonyManager mTelephonyMgr;
			mTelephonyMgr = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			return mTelephonyMgr.getLine1Number();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @param context
	 * @return
	 */
	public static String getMac(Context context) {
		try {
			WifiManager wifi = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);

			WifiInfo info = wifi.getConnectionInfo();

			return info.getMacAddress();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 
	 * @param context
	 */
	public static long getTotalMemorySize(Context context) {
		String dir = "/proc/meminfo";
		try {
			FileReader fr = new FileReader(dir);
			BufferedReader br = new BufferedReader(fr, 2048);
			String memoryLine = br.readLine();
			String subMemoryLine = memoryLine.substring(memoryLine
					.indexOf("MemTotal:"));
			br.close();
			return Integer.parseInt(subMemoryLine.replaceAll("\\D+", "")) * 1024l;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * @param context
	 * @param Id
	 * @return
	 */
	public static String getRawFileContent(Context context, int Id) {
		String ret = "";
		try {
			InputStream is = context.getResources().openRawResource(Id);
			int len = is.available();
			byte[] buffer = new byte[len];
			is.read(buffer);
			ret = EncodingUtils.getString(buffer, "utf-8");
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static String getRawFileContent(Context context, String name) {
		String ret = "";
		try {
			int id = context.getResources().getIdentifier(name, "raw",
					context.getPackageName());
			InputStream is = context.getResources().openRawResource(id);
			int len = is.available();
			byte[] buffer = new byte[len];
			is.read(buffer);
			ret = EncodingUtils.getString(buffer, "utf-8");
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(ret==null){
			ret="";
		}
		return ret;
	}



	public static int getCurrentNetType(Context context) {
		int type = 0;
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		if (info == null) {
			type = 0;
		} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
//			type = "wifi";
			type = 1;
		} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
			int subType = info.getSubtype();
			if (subType == TelephonyManager.NETWORK_TYPE_CDMA
					|| subType == TelephonyManager.NETWORK_TYPE_GPRS
					|| subType == TelephonyManager.NETWORK_TYPE_EDGE) {
//				type = "2g";
				type = 2;
			} else if (subType == TelephonyManager.NETWORK_TYPE_UMTS
					|| subType == TelephonyManager.NETWORK_TYPE_HSDPA
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_A
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_0
					|| subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
//				type = "3g";
				type = 3;
			} else if (subType == TelephonyManager.NETWORK_TYPE_LTE) {// LTE是3g到4g的过渡，是3.9G的全球标准
//				type = "4g";
				type = 4;
			}else{
				type=2;
			}
		}
		return type;
	}

	public static int isDeviceRooted() {
		if (checkRootMethod1()) {
			return 1;
		}
		if (checkRootMethod2()) {
			return 1;
		}
		if (checkRootMethod3()) {
			return 1;
		}
		return 0;
	}

	private static boolean checkRootMethod1() {
		String buildTags = android.os.Build.TAGS;

		if (buildTags != null && buildTags.contains("test-keys")) {
			return true;
		}
		return false;
	}

	private static boolean checkRootMethod2() {
		try {
			File file = new File("/system/app/Superuser.apk");
			if (file.exists()) {
				return true;
			}
		} catch (Exception e) {
		}

		return false;
	}

	private static boolean checkRootMethod3() {
		if (executeCommand(SHELL_CMD.check_su_binary) != null) {
			return true;
		} else {
			return false;
		}
	}

	private static ArrayList<String> executeCommand(SHELL_CMD shellCmd) {
		String line = null;
		ArrayList<String> fullResponse = new ArrayList<String>();
		Process localProcess = null;
		try {
			localProcess = Runtime.getRuntime().exec(shellCmd.command);
		} catch (Exception e) {
			return null;
		}
		// BufferedWriter out = new BufferedWriter(new
		// OutputStreamWriter(localProcess.getOutputStream()));
		BufferedReader in = new BufferedReader(new InputStreamReader(
				localProcess.getInputStream()));

		try {
			while ((line = in.readLine()) != null) {
				fullResponse.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fullResponse;
	}

	public static enum SHELL_CMD {
		check_su_binary(new String[] { "/system/xbin/which", "su" }), ;
		String[] command;

		SHELL_CMD(String[] command) {
			this.command = command;
		}
	}

}
